﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Excel = Microsoft.Office.Interop.Excel;

namespace xlsxDataCol.Core
{
    class ExcelReader
    {

        public ExcelReader()
        {
        }

        public strctSheet Open(string szFileName)
        {
            strctSheet ret = new strctSheet();
            Excel.Application excelApp = new Excel.Application();
            excelApp.Visible = false;
            Excel.Workbook wrkBook = excelApp.Application.Workbooks.Open(szFileName);
            Excel.Worksheet wrkSheet = wrkBook.ActiveSheet;

            ret.MainClass = wrkSheet.Cells[2, "B"].Text;
            ret.SecClass = wrkSheet.Cells[2, "D"].Text;
            ret.Date = wrkSheet.Cells[2, "F"].Text;
            ret.Subject = wrkSheet.Cells[3, "F"].Text;
            ret.Topic = wrkSheet.Cells[4, "B"].Text;

            // Mode
            ret.Mode = enumMode.Null;
            if (((string)wrkSheet.Cells[8, "A"].Text).Length > 0)
            {
                ret.Mode = enumMode.One2One;
            }
            else if (((string)wrkSheet.Cells[8, "B"].Text).Length > 0)
            {
                ret.Mode = enumMode.One2Two;
            }
            else if (((string)wrkSheet.Cells[8, "C"].Text).Length > 0)
            {
                ret.Mode = enumMode.One2Three;
            }

            // Weather
            ret.Weather = enumWeather.Null;
            if (((string)wrkSheet.Cells[8, "D"].Text).Length > 0)
            {
                ret.Weather = enumWeather.Fine;
            }
            else if (((string)wrkSheet.Cells[8, "E"].Text).Length > 0)
            {
                ret.Weather = enumWeather.Cloudy;
            }
            else if (((string)wrkSheet.Cells[8, "F"].Text).Length > 0)
            {
                ret.Weather = enumWeather.Rain;
            }
            else if (((string)wrkSheet.Cells[8, "G"].Text).Length > 0)
            {
                ret.Weather = enumWeather.Thunder;
            }

            //Problem
            ret.IsNormal = true;
            if (((string)wrkSheet.Cells[11, "A"].Text).Length > 0)
            {
                ret.IsNormal = false;
            }

            //Disconnect
            if (!int.TryParse(wrkSheet.Cells[15, "A"].Text, out ret.Disconnect.Times))
            {
                ret.Disconnect.Times = 0;
            }
            ret.Disconnect.Duration = wrkSheet.Cells[15, "B"].Text;

            // Video Problems

            // ---->Delay
            ret.VideoProblem.Delay = enumDelay.Null;
            if (((string)wrkSheet.Cells[15, "C"].Text).Length > 0)
            {
                ret.VideoProblem.Delay = enumDelay.OneSec;
            }
            else if (((string)wrkSheet.Cells[15, "D"].Text).Length > 0)
            {
                ret.VideoProblem.Delay = enumDelay.TwoFourSec;
            }
            else if (((string)wrkSheet.Cells[15, "E"].Text).Length > 0)
            {
                ret.VideoProblem.Delay = enumDelay.BydFiveSec;
            }

            // ---->Stuck
            if (!int.TryParse(wrkSheet.Cells[15, "F"].Text, out ret.VideoProblem.Stuck.Times))
            {
                ret.VideoProblem.Stuck.Times = 0;
            }
            ret.VideoProblem.Stuck.Duration = wrkSheet.Cells[15, "G"].Text;

            // Audio Problems

            // ---->unClear
            ret.AudioProblem.Clear = true;
            if (((string)wrkSheet.Cells[19, "A"].Text).Length > 0)
            {
                ret.AudioProblem.Clear = false;
            }

            // ---->Delay
            ret.AudioProblem.Delay = enumDelay.Null;
            if (((string)wrkSheet.Cells[19, "B"].Text).Length > 0)
            {
                ret.AudioProblem.Delay = enumDelay.OneSec;
            }
            else if (((string)wrkSheet.Cells[19, "C"].Text).Length > 0)
            {
                ret.AudioProblem.Delay = enumDelay.TwoFourSec;
            }
            else if (((string)wrkSheet.Cells[19, "D"].Text).Length > 0)
            {
                ret.AudioProblem.Delay = enumDelay.BydFiveSec;
            }

            // ---->Stuck
            if (!int.TryParse(wrkSheet.Cells[19, "E"].Text, out ret.AudioProblem.Stuck.Times))
            {
                ret.AudioProblem.Stuck.Times = 0;
            }
            ret.AudioProblem.Stuck.Duration = wrkSheet.Cells[15, "F"].Text;

            // ---->Mute
            ret.AudioProblem.Mute = false;
            if (((string)wrkSheet.Cells[18, "G"].Text).Length > 0)
            {
                ret.AudioProblem.Mute = true;
            }

            //Others
            ret.Suggest = wrkSheet.Cells[20, "B"].Text;
            ret.Analyse = wrkSheet.Cells[21, "B"].Text;
            ret.Conclusion = wrkSheet.Cells[22, "B"].Text;
            ret.Remarks = wrkSheet.Cells[7, "H"].Text;

            //Exit
            excelApp.Quit();
            return ret;
        }

    }
}
